package com.neusoft.yuejian_01.fragment;


import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.neusoft.yuejian_01.R;
import com.neusoft.yuejian_01.adapter.SetPicturelistAdapter;
import com.neusoft.yuejian_01.javabean.SetPictureBean;
import com.neusoft.yuejian_01.listener.RetrofitListener;
import com.neusoft.yuejian_01.model.SetPictureModel;

import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class MeSetPictureFragment extends Fragment implements RetrofitListener{

    private RecyclerView recyclerView;
    private  View view=null;
    private ImageView back;
    private FragmentManager manager;
    private   MeFragment meFragment;

    public MeSetPictureFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view=inflater.inflate(R.layout.fragment_me_setpic,container,false);
        recyclerView = (RecyclerView) view.findViewById(R.id.picture_recycleView);
        back= (ImageView) view.findViewById(R.id.sreturn);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        initData();
        initEvents();
        return view;
    }

    private void initEvents() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View view) {
                meFragment = new MeFragment();
                replace( meFragment);
            }
        });
    }

//我的发帖列表
    private void initData() {

        SetPictureModel setPictureModel = new SetPictureModel();
        setPictureModel.getMePostlist(getUser_id(),this);
 }
    public int getUser_id()
    {
        SharedPreferences sp;
        sp =getActivity().getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        return sp.getInt("u_id", -1);
    }
    private void replace(Fragment fragment) {
        manager = getChildFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.setpiocinf, fragment);
        transaction.commit();
    }

    @Override
    public void onSuccess(Object object, int flag) {
        List<SetPictureBean> SetPicture = (List<SetPictureBean>)object;
        recyclerView.setAdapter(new SetPicturelistAdapter(getActivity(),SetPicture));
    }

    @Override
    public void onFail() {
    }
}
